package com.ruoze.bdp.plugin;

import com.ruoze.bdp.entity.datasource.BaseDataSourceParamDTO;
import com.ruoze.bdp.enums.DbType;
import com.ruoze.bdp.function.DataSourceProcessor;
import com.ruoze.bdp.utils.JSONUtils;
import com.google.gson.JsonObject;
import com.ruoze.bdp.function.ConnectionParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

@Deprecated
public class DataSourceUtils {

    public DataSourceUtils() {
    }

    private static final Logger logger = LoggerFactory.getLogger(DataSourceUtils.class);

    public static BaseDataSourceParamDTO buildDatasourceParam(String param) {
        JsonObject jsonObject = JSONUtils.GSON.fromJson(param, JsonObject.class);
        return getDatasourceProcessor(DbType.ofName(jsonObject.get("type").getAsString().toUpperCase())).castDatasourceParamDTO(param);
    }

    public static BaseDataSourceParamDTO buildDatasourceParamDTO(DbType dbType, String connectionParams) {
        return getDatasourceProcessor(dbType).createDatasourceParamDTO(connectionParams);
    }

    public static DataSourceProcessor getDatasourceProcessor(DbType dbType) {
        Map<String, DataSourceProcessor> dataSourceProcessorMap = DataSourceProcessorManager.getInstance().getDataSourceProcessorMap();
        if (!dataSourceProcessorMap.containsKey(dbType.name())) {
            throw new IllegalArgumentException("illegal datasource type");
        }
        return dataSourceProcessorMap.get(dbType.name());
    }

    public static ConnectionParam buildConnectionParams(BaseDataSourceParamDTO baseDataSourceParamDTO) {
        ConnectionParam connectionParams = getDatasourceProcessor(baseDataSourceParamDTO.getType())
                .createConnectionParams(baseDataSourceParamDTO);
        logger.info("parameters map:{}", connectionParams);
        return connectionParams;
    }
}
